import { ApiResponse, User, UserProfile } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'

export function getUser ():RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    console.log(res, 991)
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data
    })
  }
}

export const getUserProfile = ():RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<UserProfile>>('/user/profile')
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data
    })
  }
}

// 重新发送请求修改个人信息
export function updateUserProfile (type:string, value:string):RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/profile', {
      [type]: value
    })
    // 重新渲染
    dispatch(getUserProfile())
  }
}
// 更新头像
export const updateUserPhoto = (data: FormData): RootThunkAction => {
  return async (dispatch) => {
    await request.patch('/user/photo', data)

    // 重新渲染
    dispatch(getUserProfile())
  }
}
